package com.huaxin.hxcloud.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huaxin.common.Constants;
import com.huaxin.common.media.view.PhotoAddView;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.base.HxBaseDialogFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author songxf
 * @date 2018/5/28
 * @Desc 站点图片上传弹出dialogFragment
 * Copyright HUAXIN DATA. All rights reserved.dialog_commit
 */
public class HxSationDialogFragment extends HxBaseDialogFragment {
    @BindView(R.id.tv_title)
    TextView       mTvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout mRlTitle;
    @BindView(R.id.et_des)
    EditText       mEtDes;
    @BindView(R.id.photo_add_view)
    PhotoAddView   mPhotoAddView;
    @BindView(R.id.rl_content)
    LinearLayout   mRlContent;
    @BindView(R.id.bt_cancel)
    Button         mBtCancel;
    @BindView(R.id.bt_ok)
    Button         mBtOk;
    @BindView(R.id.rl_button)
    LinearLayout   mRlButton;
    private CancleLisetener mCancleLisetener;
    private UpLoadLisetener mLoadLisetener;
    private String[]        imgList, compress;
    private int    maxSize = 4;
    private String title   = "添加备注";
    private boolean isRecorderVideo;
    private boolean isVisiablePhoto;
    private boolean isVideo = false;

    public interface CancleLisetener {
        void cancle();
    }

    public interface UpLoadLisetener {
        void upload();
    }

    public static HxSationDialogFragment newInstance() {
        Bundle args = new Bundle();
        HxSationDialogFragment fragment = new HxSationDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }


    public void show(FragmentManager manager, String tag) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }

    public void setTitle(String title) {
        if (mTvTitle != null) {
            mTvTitle.setText(title);
        }
        this.title = title;
    }

    @Override
    public int setRootViewId() {

        return R.layout.dialog_commit;
    }

    @Override
    public void initData() {
        mPhotoAddView.setDatas(imgList, compress);
        mPhotoAddView.setMaxSize(maxSize);
        mPhotoAddView.isRecorderVideo(isRecorderVideo);
        mTvTitle.setText(title);
        Bundle bundle = getArguments();
        //取消文本
        String cancleStr = bundle.getString(Constants.DIALOG_CANCLE_STR);
        //确定文本
        String uploadbtnStr = bundle.getString(Constants.DIALOG_UPLOADBTN_STR);
        //是否显示图片
        boolean isPhone = bundle.getBoolean(Constants.DIALOG_IS_PHONE);
        //是否上传视频
        boolean isVideo = bundle.getBoolean(Constants.DIALOG_IS_VIDEO);
        setTextBotton(cancleStr, uploadbtnStr);
      //  setVisiablePhoto(isPhone);
        isRecorderVideo(isVideo);
    }

    @Override
    public int showPosition() {
        return Gravity.CENTER;
    }

    public void setImgList(String[] imgList, String[] compress) {
        this.imgList = imgList;
        this.compress = compress;
        if (mPhotoAddView != null) {
            mPhotoAddView.setDatas(imgList, compress);
        }
    }

    public String[] getImgs() {
        if (mPhotoAddView == null) {
            return null;
        }
        return mPhotoAddView.getCompressPaths();
    }

    public boolean isVideo() {
        if (mPhotoAddView != null) {
            this.isVideo = mPhotoAddView.isVideo();
        }
        return this.isVideo;
    }


    public void setCancleLisetener(CancleLisetener cancleLisetener) {
        this.mCancleLisetener = cancleLisetener;
    }

    public void setLoadLisetener(UpLoadLisetener uploadLisetener) {
        this.mLoadLisetener = uploadLisetener;
    }

    @OnClick({R.id.bt_cancel, R.id.bt_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_cancel:
                if (mCancleLisetener != null) {
                    mCancleLisetener.cancle();
                } else {
                    dismiss();
                }

                break;
            case R.id.bt_ok:
                if (mLoadLisetener != null) {
                    mLoadLisetener.upload();
                }
                break;
        }
    }

    public String getDes() {
        return mEtDes.getText().toString();
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        if (mPhotoAddView != null) {
            mPhotoAddView.setMaxSize(maxSize);
        }
    }

    private void isRecorderVideo(boolean isRecorderVideo) {
        this.isRecorderVideo = isRecorderVideo;
        if (mPhotoAddView != null) {
            mPhotoAddView.isRecorderVideo(isRecorderVideo);
        }
    }


    private void setVisiablePhoto(boolean isVisiablePhoto) {
        this.isVisiablePhoto = isVisiablePhoto;
        if (mPhotoAddView != null) {
            if (isVisiablePhoto) {
                mPhotoAddView.setVisibility(View.VISIBLE);
            } else {
                mPhotoAddView.setVisibility(View.GONE);
            }
        }
    }

    private void setTextBotton(String cancle, String confirm) {
        if (!TextUtils.isEmpty(cancle) && !TextUtils.isEmpty(confirm)) {
            this.mBtCancel.setText(cancle);
            this.mBtOk.setText(confirm);
        }
    }
}
